package edu.nepu.bigdata.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeUtils {

    public static String timestampToString(long timestamp, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(timestamp));
    }

    /**
     * 将时间字符串转换为时间戳
     *
     * @param time   时间字符串，例如 "2021-01-01 00:00:00"
     * @param format 时间格式，例如 "yyyy-MM-dd HH:mm:ss"
     * @return 时间戳，单位为毫秒
     */
    public static long stringToTimestamp(String time, String format) throws ParseException {
        DateFormat sdf = new SimpleDateFormat(format);
        Date date = sdf.parse(time);
        return date.getTime();
    }
}
